package com.cm.sys.system.service.impl;

import com.cm.sys.system.dao.*;
import com.cm.sys.system.entity.BusinessInfo;
import com.cm.sys.system.entity.Menu;
import com.cm.sys.system.entity.User;
import com.cm.sys.system.entity.UserRole;
import com.cm.sys.system.service.IndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Creator:郭兆鹏
 * Date:2020/1/14
 * 作用:首页信息展示
 */
@Service
public class IndexServiceImpl implements IndexService {

    private static Logger logger = LoggerFactory.getLogger(IndexServiceImpl.class);

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BusinessInfoMapper businessInfoMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuRelationMapper relationMapper;

    @Autowired
    private RoleAuthMapper roleAuthMapper;
    @Value("${BusinessMenu}")
    private String BusinessMenu;

    @Override
    public Integer getRoleCount() {
        return roleMapper.selectCount();
    }

    @Override
    public Integer getUserCount() {
        return userMapper.selectCount();
    }

    @Override
    public List<BusinessInfo> getBusinessCount() {
        return businessInfoMapper.selectAllBusiness();
    }

    @Override
    public Integer getRoleByUser(Integer id) {
        return userRoleMapper.selectRoles(id).size();
    }

    @Override
    public Integer getWorkMateCount(Integer id) {
        return  userRoleMapper.selectWorkMates(id);
    }

    @Override
    public List<BusinessInfo> getBusinessByUser(Integer id) {
        List<UserRole> userRoles = userRoleMapper.selectRoles(id);
        List<Integer> roleIds = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            roleIds.add(userRole.getRoleId());
        }
        //得到roleid的集合，根据此集合找到该用户对应的所有权限菜单
        List<Integer> menuIds = roleAuthMapper.selectMenuByRoleIds(roleIds);
        //创建返回的结果集
        List<BusinessInfo> resultMenu=new ArrayList<>();
        //根据权限菜单找出对应的业务线菜单，
        List<BusinessInfo> allBusiness = businessInfoMapper.selectAllBusiness();
        for (Integer menuId : menuIds) {
            for (BusinessInfo business : allBusiness) {
                if(business.getMenuId().equals(menuId)){
                    resultMenu.add(business);
                }
            }
        }
        return resultMenu;
    }

}
